import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Table } from 'antd';
import styles from './CommonTable.less';

export default class CommonTable extends Component {

    constructor(props) {
        super(props);
        this.state = {};
        this.initPageInfo = {
            showQuickJumper: true,
            showTotal(total){
                return <div>共{total}条</div>;
            }
        }
    }

    render() {
        const { dataSource, columns, pagination, handlePageChange, loading, title, pageSize, rowSelection, size } = this.props;
        const pageInfo = pagination ?  { ...pagination, ...this.initPageInfo, pageSize } : false;
        return (
            <div className={styles.tableContainer}>
                <Table
                    loading={loading}
                    dataSource={dataSource}
                    bordered
                    columns={columns}
                    rowSelection={rowSelection}
                    rowKey={'id'}
                    onChange={handlePageChange}
                    locale={{emptyText: '暂无数据'}}
                    title={title}
                    size={size}
                    pagination={pageInfo}/>
            </div>

        );
    }
}

CommonTable.propTypes = {
    dataSource: PropTypes.array,
    columns: PropTypes.array.isRequired,
    pagination: PropTypes.object,
    loading: PropTypes.bool,
    handlePageChange: PropTypes.func,
    title: PropTypes.any,
    pageSize: PropTypes.number,
    rowSelection: PropTypes.object,
    size: PropTypes.string,
};
CommonTable.defaultProps = {
    dataSource: [],
    loading: false,
    pagination: {
    },
    handlePageChange() {
    },
    title: null,
    pageSize: 10,
    rowSelection: null,
    size: 'default'
}
